package org.czl.leetCode1;

public class L112 {
    int goal = 0;
    boolean ans = false;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        dfs(root, targetSum);
        return ans;
    }

    void dfs(TreeNode root, int targetSum){
        if(root == null)return;
        goal += root.val;
        if(root.left == null && root.right == null){
            if(goal == targetSum) ans = true;
        }
        dfs(root.left, targetSum);
        dfs(root.right, targetSum);
        goal -= root.val;

    }
}
